﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SH.STD.DataProvider;
using SH.STD.Model;

namespace SH.STD.Web.Achievement
{
    public partial class AwardList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Master.SetTab(TabType.Achievement);

            if (!Page.IsPostBack)
            {
                var sql = @"
select a.ID,a.Name,a.NO,a.Type,a.ReceiveDate,a.DepID,b.Name as DepName
from Award a
join Department b
on a.DepID=b.ID
where a.status=1
order by a.ID";

                this.BindData(sql);
                this.BindDepartment();
            }
        }

        private void BindData(string sql)
        {
            var dt = Cmn.DB.getDataTable(sql);

            this.rptAwards.DataSource = dt;
            this.rptAwards.DataBind();
            this.lblCnt1.Text = dt.Rows.Count.ToString();
        }

        private void BindDepartment()
        {
            var sql = "select ID,Name from Department where Status=1 order by ID";
            this.ddlDepartment.Items.Clear();
            this.ddlDepartment.Items.Add(new ListItem("-请选择-", "-1"));
            var dt = Cmn.DB.getDataTable(sql);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.ddlDepartment.Items.Add(new ListItem(dt.Rows[i][1].ToString(), dt.Rows[i][0].ToString()));
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            var sql = @"
select a.ID,a.Name,a.NO,a.Type,a.ReceiveDate,a.DepID,b.Name as DepName
from Award a
join Department b
on a.DepID=b.ID
where a.status=1 and ";
            var where = String.Empty;
            if (this.ddlDepartment.SelectedIndex > 0)
            {
                where += "a.DepID=" + this.ddlDepartment.SelectedValue + " and ";
            }
            if (!String.IsNullOrEmpty(this.txtReceiveStartDate.Text.Trim()))
            {
                where += "a.ReceiveDate>='" + this.txtReceiveStartDate.Text.Trim() + "' and ";
            }
            if (!String.IsNullOrEmpty(this.txtReceiveEndDate.Text.Trim()))
            {
                where += "a.ReceiveDate<='" + this.txtReceiveEndDate.Text.Trim() + "' and ";
            }
            if (!String.IsNullOrEmpty(this.txtNo.Text.Trim()))
            {
                where += "a.No like '%" + this.txtNo.Text.Trim() + "%' and ";
            }
            if (!String.IsNullOrEmpty(this.txtName.Text.Trim()))
            {
                where += "a.Name like '%" + this.txtName.Text.Trim() + "%' and ";
            }
            if (!String.IsNullOrEmpty(this.txtMemberName.Text.Trim()))
            {
                where +=
@"a.ID in 
(   
    select a.ID 
    from Member a 
    join Employee b
    on a.EmployeeID=b.ID
    where a.TableID=3 and b.Name like '%" + this.txtMemberName.Text.Trim() + @"%'
) and ";
            }
            if (!String.IsNullOrEmpty(this.txtName.Text.Trim()))
            {
                where += "a.Name like '%" + this.txtName.Text.Trim() + "%' and ";
            }
            sql = where == String.Empty ? sql.Substring(0, sql.LastIndexOf("and ")) : sql + " " + where.Substring(0, where.LastIndexOf("and "));

            this.BindData(sql);
        }
        protected void btnClear_Click(object sender, EventArgs e)
        {
            this.txtName.Text = String.Empty;
            this.txtMemberName.Text = String.Empty;
            this.txtNo.Text = String.Empty;
            this.txtReceiveStartDate.Text = String.Empty;
            this.txtReceiveEndDate.Text = String.Empty;
            this.ddlDepartment.SelectedIndex = -1;

            var sql = @"
select a.ID,a.Name,a.NO,a.Type,a.ReceiveDate,a.DepID,b.Name as DepName
from Award a
join Department b
on a.DepID=b.ID
where a.status=1
order by a.ID";

            this.BindData(sql);
        }
    }
}